// pages/settings/modifybuyer/modifybuyer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityName: '',
    commodityId: '',
    commodityBarcode:'',
    commodityCost:'',
    commoditySeller:'',
    commodityUnit_conversion:'',
    category: ["冷冻", "常温"],
    index: '',
    isnull_name: false,
    isnull_cost: false,
    isnull_unit: false
  },

  bindChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail)
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      commodityName: options.commodityName,
      commodityId: options.commodityId,
      commodityBarcode: options.commodityBarcode,
      commodityCost: Number(options.commodityCost),
      commoditySeller: options.commoditySeller,
      commodityUnit_conversion: options.commodityUnit_conversion
    })
    if (options.commodityCategory == "常温"){
      this.setData({index : 1})
    }
    else{
      this.setData({ index: 0 })
    }
  },
  showTopTips: function () {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 1500);
  },



  formSubmit(e) {
    var that = this
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.is_null(e)){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          wx.request({
            url: 'http://192.168.3.2/cloudwarehouse/settings/commodity/commoditymanagement/modifycommodity.php',
            data: {
              name: e.detail.value.name,
              cost: e.detail.value.cost,
              unit: e.detail.value.unit,
              category: e.detail.value.category,
              seller: e.detail.value.seller,
              barcode: e.detail.value.barcode,
              openid: res.data,
              id : that.data.commodityId
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success(res) {
              if (!res.data) {
                wx.showToast({
                  title: '商品名重复',
                  image: '/icons/stop.png'
                })
                return;
              } else{
                wx.showToast({
                  title: '更改成功',
                })
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1000)}
            }
          })
        },
      })
    }
  },

  is_null: function (e) {
    var that = this
    if (e.detail.value.cost != '' && e.detail.value.name != '' && e.detail.value.unit != '')
      return 1;
    else if (e.detail.value.name == '') {
      that.setData({
        isnull_name: true
      })
    } else (that.setData({
      isnull_name: false
    }))

    if (e.detail.value.cost == '') {
      that.setData({
        isnull_cost: true
      })
    } else (that.setData({
      isnull_cost: false
    }))

    if (e.detail.value.unit == '') {
      that.setData({
        isnull_unit: true
      })
    } else (that.setData({
      isnull_unit: false
    }))
    this.showTopTips()
    return 0;

  },




  scan: function () {
    var that = this
    wx.scanCode({
      scanType: ['barCode'],
      success(res) {
        console.log(res.result)
        that.setData({
          commodityBarcode: res.result
        })
      }
    })
  },

  del: function () {
    var that = this
    wx.showModal({
      title: '确认删除',
      content: '确认要删除这件商品吗？',
      confirmText: "确认",
      cancelText: "取消",
      confirmColor: '#e64340',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'http://192.168.3.2/cloudwarehouse/settings/commodity/commoditymanagement/deletecommodity.php',
            data: { id: that.data.commodityId },
            success(res) {
              console.log(res)
              wx.showToast({
                title: '删除成功',
              })
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})